package com.hh.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.*;
import com.hh.service.VideoService;
import com.hh.util.InitClient;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;

@Service
public class VideoServiceImpl implements VideoService {

    DefaultAcsClient client = InitClient.initVodClient();

    @Override
    public HashMap<String, String> getPlayInfo(String videoId) {
        GetPlayInfoRequest request = new GetPlayInfoRequest();
        GetPlayInfoResponse response = null;
        GetVideoPlayAuthRequest requestAuth = new GetVideoPlayAuthRequest();
        GetVideoPlayAuthResponse responseAuth = new GetVideoPlayAuthResponse();
        HashMap<String, String> result = new HashMap<String, String>();

        request.setVideoId(videoId);
        requestAuth.setVideoId(videoId);
        try {
            response = client.getAcsResponse(request);
            responseAuth = client.getAcsResponse(requestAuth);
            List<GetPlayInfoResponse.PlayInfo> playInfoList = response.getPlayInfoList();
            for (GetPlayInfoResponse.PlayInfo playInfo : playInfoList) {
                if (playInfo != null) {
                    result.put("videoUrl", playInfo.getPlayURL());
                    result.put("videoTitle", response.getVideoBase().getTitle());
                    result.put("videoAuth", responseAuth.getPlayAuth());
                }
            }
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public String uploadVideo(MultipartFile file) {
        try {
            String accessKeyId = "LTAI5tSTKECgQvx1Ui4Y85MR";
            String accessKeySecret = "g3B9XKHIZWSHC92xoLDKJgoYR2AA3F";
            String fileName = file.getOriginalFilename();
            String title = fileName.substring(0, fileName.lastIndexOf("."));
            InputStream inputStream = file.getInputStream();

            UploadStreamRequest request = new UploadStreamRequest(accessKeyId, accessKeySecret, title, fileName, inputStream);
            UploadVideoImpl uploader = new UploadVideoImpl();
            UploadStreamResponse response = uploader.uploadStream(request);
            return response.getVideoId();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public void deleteUploadVideo(String videoIds) {
        try {
            DeleteVideoRequest request = new DeleteVideoRequest();
            request.setVideoIds(videoIds);
            DeleteVideoResponse acsResponse = client.getAcsResponse(request);
            System.out.println(acsResponse.getRequestId());
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }

    @Override
    public String getPlayAuth(String vid) {
        String accessKeyId = "LTAI5tSTKECgQvx1Ui4Y85MR";
        String accessKeySecret = "g3B9XKHIZWSHC92xoLDKJgoYR2AA3F";
        DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai", accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);
        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
        // 视频ID，可以通过GetVideoList接口查询得到
        request.setVideoId(vid);
        String auth = "";
        try {
            GetVideoPlayAuthResponse response = client.getAcsResponse(request);
            auth = response.getPlayAuth();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
        return auth;
    }
}
